#!/usr/bin/python

"""Project Euler Solution 085

Copyright (c) 2011 by Robert Vella - robert.r.h.vella@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

import cProfile
from euler.list_functions import max_by

def get_answer():
    """Question:
    
    By counting carefully it can be seen that a rectangular grid measuring 3 
    by 2 contains eighteen rectangles.

    Although there exists no rectangular grid that contains exactly two 
    million rectangles, find the area of the grid with the nearest solution.
    """
    
    """
    The simplest equation to determine the number of rectangles that can be 
    fitted into a grid is as follows.
    
    number_of_rects(x, y) = sum(i for i in range(1, x + 1)) 
                                * sum(i for i in range(1, y + 1))
    
    Where 
     x = the width of the grid.
     y = the height of the grid.
    
    This can be easily shown using the example given in the problem 
    description:
     
    There are:
    
    6 1*1 rectangles 
    4 2*1 rectangles
    2 3*1 rectangles
    
    3 1*2 rectangles
    2 2*2 rectangles
    1 3*2 rectangle
    
    = 18 rectangles
    
    
    The same result can be begot by multiplying 
    the (grid width - rectangle width + 1) by the 
    (grid height - rectangle height + 1) for each rectangle and summing 
    the result:
    
    3-1+1 * 2-1+1 = 6 rectangles 
    3-2+1 * 2-1+1 = 4 rectangles
    
    ...
    
    3-3+1 * 2-2+1 = 1 rectangle
    
    = 18 rectangles
    
    Coincidentally, because of the nature of this problem -- that it covers 
    all possible areas -- the above problem can be simplified as follows:
    
    1 * 1 = 1
    2 * 1 = 2
    
    ...
    
    2 * 2  = 4
    3 * 2 = 6
    
    = 18 
    
    By considering the above we get the function.
        
    number_of_rects(x, y) = sum(i for i in range(1, x + 1)) 
                                * sum(i for i in range(1, y + 1))
    
    This function can be simplified further.
    
    number_of_rects(x, y) = (x(x + 1))/2 * (y(y + 1))/2
    
    Therefore we only need to maximize the following inequality:
        
    (x(x + 1))/2 * (y(y + 1))/2 < 2,000,000
    x, y > 0
    
    Also:
     
    x <= y 
    because number_of_rects(x, y) == number_of_rects(y, x).
    
    y < sqrt(2,000,000) 
    because number_of_rects(sqrt(2,000,000), sqrt(2,000,000)) > 2,000,000
    """
     
   
    def number_of_rects(x, y):
        """Returns the number of rectangles which fit into a grid of [x]by[y]
        cells.
        """
        
        return int((x * (x + 1)) / 2 * (y * (y + 1)) / 2)
    
                    
    #The combination of x and y for which the number of rectangles
    #comes closest to 2,000,000.                    
    max_x_y = max_by(
                 lambda xy : xy["number_of_rects"],
                 ({"x": x, "y": y, "number_of_rects": number_of_rects(x, y)} 
                    for x in range(int(2000000 ** 0.5))
                    for y in range(x + 1)
                    if number_of_rects(x, y) < 2000000)
                )
    
    #Return the result.
    return max_x_y["x"] * max_x_y["y"]
            

if __name__ == "__main__":
    cProfile.run("print(get_answer())")
